// pages/productDetail/productDetail.js
import * as API_Product from '../../api/product'
import * as API_Comment from '../../api/comment'
import * as API_Collect from '../../api/collect'
import * as API_Order from '../../api/order'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    saleState: true,
    productId: '',
    product: {},
    mainAttr: [],
    mutipleAttr: [],
    commentList: [],
    starList: [, , , , , ],
    params: {
      pageNum: 1,
      pageSize: 3,
      storeId: '',
      productId: '',
      pid: 0
    },
    finished: false,
    total: 0,
    collect: false,
    collectModel: {},
    type: 0
  },
  autoplayVideo: function (e) {
    let indexAll = e.currentTarget.dataset.indexAll;
    let commentList = this.data.commentList;
    commentList[indexAll].autoplay = true;
    this.setData({
      commentList: commentList
    });
    // debugger
  },
  toCommentList: function (e) {
    let storeId = this.data.params.storeId;
    let productId = this.data.params.productId;
    wx.navigateTo({
      url: '/pages/commentList/commentList?storeid=' + storeId + '&productId=' + productId,
    })
  },
  goMessageDetail: function (e) {
    let storeId = this.data.params.storeId;
    wx.navigateTo({
      url: '../messageDetail/messageDetail?storeId=' + storeId
    })
  },
  getCollectNnum () {
    getApp().https.ajax({
      method: 'post',
      url: '/collect/getCollectNnum',
      isJson: true,
      data: {
        collectType: 'product',
        busId: this.data.params.productId
      }
    }).then(res => {
      this.setData({
        collectNnum: res.data
      })
    })
  },
  getDetail (options) {
    let data = {
      id: parseInt(options.id)
    }
    let url = '/product/getInfoTrue'
    if (options.storeId) {
      data.storeId = parseInt(options.storeId)
      url = '/product/getInfo'
    }
    getApp().https.ajax({
      method: 'post',
      url: url,
      data: data,
      loading: true
    }).then(res => {
      this.choiceGuige = this.selectComponent("#view")
      this.choiceGuige.init(res.data.infoId)
      this.choicePeopleGuige = this.selectComponent("#peopleArray")
      let r = {
        key: 'jy_people_right',
        title: '',
        selected: res.data.peopleRight
      }
      this.setData({
        'params.storeId': res.data.storeId || parseInt(options.storeId),
        'params.productId': options.id,
        productId: options.id
      }, () => {
        if (this.data.type === 0) {
          this.getCollectNnum()
        }
        this.addFoot()
      })
      if (this.data.type === 0) {
        this.getList()
      }
      this.choicePeopleGuige.loadDic(r)
      let arr = []
      let mainArr = []
      let item = JSON.parse(res.data.itemDetail)
      for (var i = 0; i < item.length; i++) {
        let attr = item[i]
        if (attr.type == 'mainAttr') {
          mainArr.push(attr)
        }
        if (attr.type == 'multipleAttr') {
          arr.push(attr)
        }
      }
      this.setData({
        product: res.data,
        mutipleAttr: arr,
        mainAttr: mainArr
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    API_Collect.query(options.id, 'product').then(res => {
      if (res.data) {
        this.setData({
          collect: true,
          collectModel: res.data,
        })
      }
    })
    this.setData({
      type: parseInt(options.type) || 0
    })
    this.getDetail(options)
  },
  goStore(e) {
    let id = this.data.params.storeId
    wx.navigateTo({
      url: '/pages/storeHome/storeHome?id=' + id,
    })
  },
  preview(event) {
    let currentUrl = event.currentTarget.dataset.src
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: event.currentTarget.dataset.srcs // 需要预览的图片http链接列表
    })
  },
  collect() {
    let params = {
      collectType: 'product',
      busId: this.data.params.productId,
      classifyId: this.data.product.classifyId,
      storeId: this.data.product.storeId
    }
    API_Collect.add(params).then(res => {
      if (res.code == 200) {
        let flag = this.data.collect
        this.setData({
          collect: !flag,
          collectModel: res.data
        })
        this.getCollectNnum()
        wx.showToast({
          title: '收藏成功',
        })
      }
    })
  },
  addFoot() {
    let params = {
      footprintType: 'product',
      busId: this.data.params.productId,
      classifyId: this.data.product.classifyId,
      storeId: this.data.params.storeId
    }
    API_Collect.addFoot(params).then(res => {})
  },
  disCollect() {
    API_Collect.delCollect(this.data.collectModel.id).then(res => {
      if (res.code == 200) {
        let flag = this.data.collect
        this.setData({
          collect: !flag,
          collectModel: {}
        })
        this.getCollectNnum()
        wx.showToast({
          title: '取消收藏成功',
        })
      }
    })
  },
  toDetail(e) {
    let that = this
    wx.navigateTo({
      url: '../commentDetail/commentDetail?img=' + that.data.product.productImage + '&storeName=' + that.data.product.storeName + '&commentId=' + e.currentTarget.dataset.id,
    })
  },
  getList() {
    API_Comment.getList(this.data.params).then(res => {
      let arr = this.data.commentList
      for (let item of res.data.rows) {
        let images = item.commentImages;

        let arrimg = [];
        if (images) {
          arrimg = images.split(",");
        }
        item.commentImages = arrimg;
        let commentVideo = item.commentVideo;
        let arrvideo = [];
        if (commentVideo) {
          arrvideo = commentVideo.split(",");
        }

        item.commentVideo = arrvideo;
        item.storeLevel = item.storeLevel.toFixed(1)

      }
      arr.push(...res.data.rows)
      this.setData({
        commentList: arr,
        finished: res.data.lastPage,
        total: res.data.total
      })
    })
  },
  sale() {
    // if (this.data.product.isVip === 'Y' && getApp().globalData.isVip !== 'Y') {
    //   wx.showToast({
    //     title: '这是会员专享商品！',
    //     icon: 'none'
    //   })
    // } else {
    //   wx.navigateTo({
    //     url: '/pages/payConfig/payConfig?productId=' + this.data.productId + '&storeId=' + this.data.params.storeId,
    //   })
    // }
    wx.navigateTo({
      url: '/pages/payConfig/payConfig?productId=' + this.data.productId + '&storeId=' + this.data.params.storeId,
    })
  },
  evaluateVideo() {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // if (this.data.finished) {
    //   return
    // }
    // var page = this.data.params.pageNum + 1
    // this.setData({
    //   'params.pageNum': page
    // })
    // this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})